
import {getCurrentInstance} from 'vue'
import tool from '@/utils/tool'
import {useRoute} from "vue-router";

export const instanceFor = (i)=> {
  let name = i?.type?.name;

  let list = ['LayoutContent','AsyncComponentWrapper','BaseTransition','Modal',undefined,'','TabPane','CollapseItem','Collapse','Tabs','GridItem','Grid','Space','ClientOnly','Drawer']
  if(!list.includes(name) && /:/.test(name)){
    return name
  }else{
    return i?.parent !== undefined && instanceFor(i?.parent)

  }
}


//判断列表缓存
export const localColumns = (columnsData,status,type)=>{
  let instance = getCurrentInstance();
  let instanceName= instanceFor(instance) || ''
  let useRouteParent=useRoute().path+':'+instanceName
  let localColumn = tool.local.get(useRouteParent)
  let crudColumn = columnsData
  let crudColumnData=[]
  if(type=='shipment'){
      if(status==1 || status==7){
        columnsData=columnsData.filter(item=>item.dataIndex != 'waybill_info' && item.dataIndex != 'ups' && item.dataIndex != 'actual_weight' && item.dataIndex != 'actual_volume' && item.dataIndex != 'wv_rate' 
          && item.dataIndex != 'income' && item.dataIndex != 'expense' && item.dataIndex != 'cost' && item.dataIndex != 'bonus' && item.dataIndex != 'flow.rough_profit' && item.dataIndex != 'flow.rough_profit_rate' && item.dataIndex != 'flow.net_profit'
          && item.dataIndex != 'flow.sale_profit' && item.dataIndex != 'supplier_desc' && item.dataIndex != 'carrier' && item.dataIndex != 'depot_id' && item.dataIndex != 'picking_at' && item.dataIndex != 'delivered_at' && item.dataIndex != 'charge_weight'
          && item.dataIndex != 'actual_volume_weight' && item.dataIndex != 'pickup_depot_id'
        )
      }else if(status==2){
          columnsData=columnsData.filter(item=>item.dataIndex != 'delivered_at')  
      }
      if(status!=''){
        columnsData=columnsData.filter(item=>item.dataIndex != 'status')  
      }
  }
  if(type=='waybillList' || type=='waybillTray' || type=='waybillTrayList'){
    if(status==1 || status==6){
      columnsData=columnsData.filter(item=>item.dataIndex != 'send' && item.dataIndex != 'tracking_no')
    }
  }
  if(type=='waybillList'){
    if(status==1){
      columnsData=columnsData.filter(item=>item.dataIndex != 'shipment.age' && item.dataIndex != 'car_number' && item.dataIndex != 'pod' && item.dataIndex != 'isa' && item.dataIndex != 'shipment.day_fee')
    }
  }
  if(type=='waybill'){
    if(status!=''){
      columnsData=columnsData.filter(item=>item.dataIndex != 'status')
    }
    if(status!=4 && status!=5 && status!=6 && status!=7){
      columnsData=columnsData.filter(item=>item.dataIndex != 'carry_at')
    }
  }
  if(!localColumn){
    return columnsData.filter(c=>c.hide)
  }
  columnsData.value = localColumn.map(i=>{
    let item= crudColumn.filter(c=>i.dataIndex === c.dataIndex)[0];
    if(item){
      item.hide = i.hide
      item.fixed = i.fixed
      crudColumnData.push(item)
    }
  })
  if(type=='shipment'){
    if(status==1 || status==7){
      columnsData=columnsData.filter(item=>item.dataIndex != 'waybill_info' && item.dataIndex != 'ups' && item.dataIndex != 'actual_weight' && item.dataIndex != 'actual_volume' && item.dataIndex != 'wv_rate' 
        && item.dataIndex != 'income' && item.dataIndex != 'expense' && item.dataIndex != 'cost' && item.dataIndex != 'bonus' && item.dataIndex != 'flow.rough_profit' && item.dataIndex != 'flow.rough_profit_rate' && item.dataIndex != 'flow.net_profit'
        && item.dataIndex != 'flow.sale_profit' && item.dataIndex != 'supplier_desc' && item.dataIndex != 'carrier' && item.dataIndex != 'depot_id' && item.dataIndex != 'picking_at' && item.dataIndex != 'delivered_at' && item.dataIndex != 'charge_weight'
        && item.dataIndex != 'actual_volume_weight' && item.dataIndex != 'pickup_depot_id'
      )
    }else if(status==2){
      crudColumnData=crudColumnData.filter(item=>item.dataIndex != 'delivered_at')  
    }
    if(status!=''){
      columnsData=columnsData.filter(item=>item.dataIndex != 'status')  
    }
  }
  if(type=='waybillList' || type=='waybillTray' || type=='waybillTrayList'){
    if(status==1 || status==6){
      columnsData=columnsData.filter(item=>item.dataIndex != 'send' && item.dataIndex != 'tracking_no')
    }
  }
  if(type=='waybillList'){
    if(status==1){
      columnsData=columnsData.filter(item=>item.dataIndex != 'shipment.age' && item.dataIndex != 'car_number' && item.dataIndex != 'pod' && item.dataIndex != 'isa' && item.dataIndex != 'shipment.day_fee')
    }
  }
  if(type=='waybill'){
    if(status!=''){
      columnsData=columnsData.filter(item=>item.dataIndex != 'status')
    }
    if(status!=4 && status!=5 && status!=6 && status!=7){
      columnsData=columnsData.filter(item=>item.dataIndex != 'carry_at')
    }
  }
  return crudColumnData.filter(c=>c.hide)
  
}

//table高度
export const tablePrantRefHeight = (tablePrantRef)=> {
  const element = tablePrantRef;
  if (element) {
    const rect = element.getBoundingClientRect();
    const remainingHeight = window.innerHeight - rect.top - 64;
    return element.style.height = remainingHeight + 'px'
  }
}

//弹框table高度
export const tablePrantRefHeightForm = (tablePrantRef)=> {
  const element = tablePrantRef;
  if (element) {
    const rect = element.getBoundingClientRect();
    const remainingHeight = window.innerHeight - rect.top - 120;
    return element.style.height = remainingHeight + 'px'
  }
}